import type { ApiResponse, PrivateMessage, Page } from "@/types";
import { get, post, put, del } from "@/utils/request";
import { MessageType } from "@/types";

/**
 * 发送私聊消息
 * @param receiverId 接收者ID
 * @param content 消息内容（文本消息时为文本内容，多媒体消息时为资源URL）
 * @param type 消息类型（0:文本 1:图片 2:语音 3:视频 4:文件）
 */
export const sendPrivateMessage = async (
  receiverId: string,
  content: string,
  type: MessageType
): Promise<ApiResponse<PrivateMessage>> => {
  return post("/messages/private", {
    receiverId,
    content,
    type,
  });
};

/**
 * 获取与指定用户的聊天记录
 * @param friendId 好友ID
 * @param page 页码，默认1
 * @param size 每页大小，默认20
 * @returns Promise<ApiResponse<Page<PrivateMessage>>> 返回一个Promise，成功时data为消息列表
 */
export const getPrivateMessages = async (
  friendId: string,
  page: number = 1,
  size: number = 20
): Promise<ApiResponse<Page<PrivateMessage>>> => {
  return get(`/messages/private/${friendId}?page=${page}&size=${size}`);
};

/**
 * 标记与指定用户的消息为已读
 * @param friendId 好友ID
 * @returns Promise<ApiResponse<void>> 返回一个Promise，成功时data为void
 */
export const markMessagesAsRead = async (
  friendId: string
): Promise<ApiResponse<void>> => {
  return put(`/messages/private/read/${friendId}`);
};

/**
 * 撤回消息
 * @param messageId 消息ID
 * @returns Promise<ApiResponse<void>> 返回一个Promise，成功时data为void
 */
export const recallMessage = async (
  messageId: string
): Promise<ApiResponse<void>> => {
  return del(`/messages/private/${messageId}`);
};
